package com.api.oa.config;

import cn.hutool.core.date.DateUtil;
import com.api.oa.model.entity.YQOperationLog;
import com.api.oa.services.YQOperationLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 系统日志：切面处理类
 */
@Aspect
@Component
public class SysLogAspect {


    @Autowired
    private YQOperationLogService interfaceTimeService;

    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation( org.springframework.web.bind.annotation.RequestMapping)")
    public void logPoinCut() {
    }

    //切面 配置通知
    @Around("logPoinCut()")         //AfterReturning
    public Object saveOperation(ProceedingJoinPoint joinPoint) throws Throwable {
        //保存日志
        long begin = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        // 记录结束时间
        long end = System.currentTimeMillis();
        long takeTime = end - begin;
        YQOperationLog operlog = new YQOperationLog();
        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();

        //获取操作--方法上的ViLog的值
        RequestMapping opLog = method.getAnnotation(RequestMapping.class);


        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 请求方式：POST/GET/other
        String httpMethod = request.getMethod();
        //操作的url
        // 客户端ip
        if (opLog != null) {
            operlog.setOperEvent(opLog.name());
            operlog.setReqType(httpMethod);
        }
        // 获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        // 获取请求的方法名
        String methodName = method.getName();
        methodName = className + "." + methodName;
        operlog.setMethodName(methodName);
        operlog.setOperTime(DateUtil.date());
        operlog.setClientIp(request.getRemoteAddr());
        operlog.setOperUrl(request.getRequestURI());
        operlog.setReqParam(request.getPathInfo());
        operlog.setHs(takeTime);
        interfaceTimeService.add(operlog);
        return result;
    }


}